package com.exiassassin;

import java.io.File;
import java.util.List;
import java.util.Vector;

import com.exiassassin.librairies.game.Coordinates;
import com.model.CoordinatesImp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

public class GameActivity extends Activity implements OnClickListener, OnTouchListener{
	/* Common Variable */
	private Resources res;
	private Intent intent;
	
	/* Class Variable */
	private Button buttonDenounce;
	private Button buttonKill;
	private Gallery galleryTarget;
	private SensorManager m;
	private LocationManager locationManager;
	/* Game Class Variable */
	private Coordinates coordinates;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gamelayout);  
		res = getResources();
		
		galleryTarget = (Gallery) findViewById(R.id.galleryTarget);
		
		Vector<Bitmap> pictureTarget = new Vector<Bitmap>();

		pictureTarget.add(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + File.separator + "tmp_picture.jpg"));
		pictureTarget.add(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + File.separator + "tmp_picture.jpg"));

		galleryTarget.setAdapter(new ImageAdapter(this, pictureTarget));

		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, moveLocation);
		
		
		m = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		
		buttonKill = (Button) findViewById(R.id.buttonKill);
		buttonKill.setOnTouchListener(this);
		
		coordinates = new CoordinatesImp();
	}

	public void onClick(View v) {
		
	}
	public void CheckAttaque(Boolean isAttacking){
		if (isAttacking)
			Log.i("GameActivity","Il faut lancer l'attaque");
	}
	public void fillCoordinates(Location location){
		coordinates.setX(location.getLatitude());
		coordinates.setY(location.getLongitude());
		coordinates.setZ(location.getAltitude());
		Log.i("GameActivity","X: "+coordinates.getX() + " / Y: "+ coordinates.getY() + " / Z: "+ coordinates.getZ());
	}
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId())
		{
		case R.id.buttonKill:
			Log.i("gameActivity","Touching..");	
			switch ( event.getAction() ) {
		    	case MotionEvent.ACTION_DOWN:	    		
		    		m.registerListener(moveSensor,  m.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
		    		Log.i("gameActivity","registerListener..");	
		    		break;
		    	case MotionEvent.ACTION_UP: 
		    		m.unregisterListener(moveSensor);
		    		Log.i("gameActivity","unregisterListener..");	
		    		break;
		    	default:
		    		
		    		break;
		    }
			break;
		}
		return false;
	}
	@Override
    public void onDestroy()
    {
		locationManager.removeUpdates(moveLocation);
        super.onDestroy();
    }
private final LocationListener moveLocation = new LocationListener() { 
	 	float x, y, z;
	    private Location location;
	    //private provider = "providerLocation";
		public void onLocationChanged(Location location) {
			Log.i("moveLocation", "onLocationChanged - ok");
			this.location = location;
			fillCoordinates(this.location);
		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		} 
   }; 	
private final SensorEventListener moveSensor = new SensorEventListener() { 
	 	float x, y, z;
	    Boolean isAttacking = false;
      
        public void onAccuracyChanged(Sensor sensor, int accuracy) {}

		public void onSensorChanged(SensorEvent event) {
			x = event.values[0];
		    y = event.values[1];
		    z = event.values[2];
		    // Log.i("MoveSensor","x: "+x+"/y: "+ y + "/z: "+z);
		    
		    if (y>18 || y< -18)
		    {
		    	Log.i("MoveSensor","ATTACK");
		    	isAttacking = true;
		    	CheckAttaque(isAttacking);
		    }
		    else
		    {
		    	Log.i("MoveSensor","NO-ATTACK");
		    	isAttacking = false;
		    }
			
		} 
   }; 
   public class ImageAdapter extends BaseAdapter {
	    int galleryItemBackground;
	    private Context mContext;
	    private List<Bitmap> pictureList;
	 
	    public ImageAdapter(Context c, List<Bitmap> pictureList) {
	        mContext = c;
	        TypedArray a = obtainStyledAttributes(R.styleable.galleryPlayerTarget);
	        galleryItemBackground = a.getResourceId(
	                R.styleable.galleryPlayerTarget_android_galleryItemBackground, 0);
	        a.recycle();
	        this.pictureList = pictureList;
	    }
	    public int getCount() {
	        return pictureList.size();
	    }
	    public Object getItem(int position) {
	        return position;
	    }
	    public long getItemId(int position) {
	        return position;
	    }
	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView i = new ImageView(mContext);
	        i.setImageBitmap((pictureList.get(position)));
	        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
	        i.setScaleType(ImageView.ScaleType.FIT_XY);
	        i.setBackgroundResource(galleryItemBackground);
	        return i;
	    }
		
	}
}
